package com.tjec.project.businessWorkbench.controller;

import java.util.List;

import com.tjec.framework.interceptor.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.LoginUser;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.businessWorkbench.domain.BmsBusinessPickup;
import com.tjec.project.businessWorkbench.domain.BmsBusinessTask;
import com.tjec.project.businessWorkbench.domain.BmsProjectDocCatalog;
import com.tjec.project.businessWorkbench.domain.ImportBmsBusinessTask;
import com.tjec.project.businessWorkbench.service.IBmsBusinessTaskService;
import com.tjec.project.businessWorkbench.service.IBmsImportExcelService;
import com.tjec.project.system.domain.BmsMdBusinessCategory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.BusinessExcelUtil;
import com.tjec.common.utils.poi.ExcelUtil;

/**
 * 任务管理Controller
 * 
 * @author pangyongfeng
 * @date 2020-04-22
 */
@Api(value="任务管理", tags = {"任务管理接口"})
@RestController
@RequestMapping("/businessWorkbench/task")
public class BmsBusinessTaskController extends BaseController
{
    @Autowired
    private IBmsBusinessTaskService bmsBusinessTaskService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBmsImportExcelService bmsImportExcelService;
    /**
     * 查询任务管理列表
     */
    @ApiOperation("查询任务管理列表")
    @PreAuthorize("@ss.hasPermi('businessWorkbench:task:list')")
    @GetMapping("/list")
    public AjaxResult list(BmsBusinessTask bmsBusinessTask)
    {
    	List<BmsBusinessTask> list = bmsBusinessTaskService.selectBmsBusinessTaskList(bmsBusinessTask);
        return AjaxResult.success(list);
    }

    /**
     * 导出任务管理列表
     */
    @ApiOperation("导出任务管理")
    @Log(title = "任务管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsBusinessTask bmsBusinessTask)
    {
        List<BmsBusinessTask> list = bmsBusinessTaskService.selectBmsBusinessTaskExportList(bmsBusinessTask);
        BusinessExcelUtil<BmsBusinessTask> util = new BusinessExcelUtil<BmsBusinessTask>(BmsBusinessTask.class);
        return util.exportExcel(list, "任务管理");
    }

    /**
     * 获取任务管理详细信息
     */
    @ApiOperation("根据guid获取任务管理详细信息")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid)
    {
        return AjaxResult.success(bmsBusinessTaskService.selectBmsBusinessTaskByGuid(guid));
    }

    /**
     * 新增任务管理
     */
    @ApiOperation("新增任务管理")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@RequestBody BmsBusinessTask bmsBusinessTask)
    {
    	LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
    	String id = user.getUser().getUserId();
    	bmsBusinessTask.setCreateBy(id);
        return toAjax(bmsBusinessTaskService.insertBmsBusinessTask(bmsBusinessTask));
    }

    /**
     * 修改任务管理
     */
    @ApiOperation("修改任务管理")
    @Log(title = "任务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsBusinessTask bmsBusinessTask)
    {
    	LoginUser user = tokenService.getLoginUser(ServletUtils.getRequest());
    	String id = user.getUser().getUserId();
    	bmsBusinessTask.setCreateBy(id);
        return toAjax(bmsBusinessTaskService.updateBmsBusinessTask(bmsBusinessTask));
    }

    /**
     * 删除任务管理
     */
    @ApiOperation("删除任务管理")
    @Log(title = "任务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(bmsBusinessTaskService.deleteBmsBusinessTaskByGuids(guids));
    }
    
    /**
     * 获取任务下拉树列表
     */
    @ApiOperation("获取任务下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BmsBusinessTask bmsBusinessTask) {
    	List<BmsBusinessTask> list = bmsBusinessTaskService.selectBmsBusinessTaskList(bmsBusinessTask);
        return AjaxResult.success(bmsBusinessTaskService.buildTaskTreeSelect(list));
    }
    
    /**
     * 导入任务管理模板
     * @throws Exception 
     */
    @ApiOperation("导入任务管理模板")
	@GetMapping("/{guid}/{businessId}")
    public AjaxResult importTaskTemplate(@PathVariable("guid") String guid,@PathVariable("businessId") String businessId) throws Exception
    {
        return toAjax(bmsImportExcelService.importTemplte(guid,"1",businessId));
    }
    
    @ApiOperation("任务管理导入清单")
    @Log(title = "任务管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,String businessId) throws Exception {
       /* ExcelUtil<ImportBmsBusinessTask> util = new ExcelUtil<ImportBmsBusinessTask>(ImportBmsBusinessTask.class);
        List<ImportBmsBusinessTask> taskList = util.importExcel(file.getInputStream());
        int res = bmsBusinessTaskService.importBmsBusinessTasks(taskList,businessId);
        return AjaxResult.success(res);*/
    	BusinessExcelUtil<BmsBusinessTask> util = new BusinessExcelUtil<BmsBusinessTask>(BmsBusinessTask.class);
    	List<BmsBusinessTask> bmsBusinessTaskList=null;
    	try {
    		bmsBusinessTaskList = util.importExcel(file.getInputStream());
		} catch (Exception e) {
			return AjaxResult.error("请导入本系统导出的自制模板文件！",100);
		}
         
         
         if(bmsBusinessTaskList==null) {
             return AjaxResult.error("请导入本系统导出的自制模板文件！",100);
         }else if(bmsBusinessTaskList.size()==0){
        	 return AjaxResult.error("导入文件为空文件！",100);
         }else {
        	 for (BmsBusinessTask bmsBusinessTask : bmsBusinessTaskList) {
            	 bmsBusinessTask.setCreateBy(tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
            	 bmsBusinessTask.setBusinessId(businessId);
     		}
             int res = bmsBusinessTaskService.importBmsBusinessTasksList(bmsBusinessTaskList);
             return AjaxResult.success(res);
         }
        
    }
}
